package com.itSession.cn.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;


public class PropUtil {
    private static Properties props;
    static{
        loadProps();
    }

    synchronized static private void loadProps(){
        props = new Properties();
        InputStream in = null;
        try {
                /*第一种，通过类加载器进行获取properties文件流*/
           in= PropUtil.class.getResourceAsStream("/config.properties");
                    /* 第二种，通过类进行获取properties文件流*/
                    //in = PropertyUtil.class.getResourceAsStream("/jdbc.properties");
                    props.load(in);
        } catch (Exception e) {

        } finally {
            try {
                if(null != in) {
                    in.close();
                }
            } catch (IOException e) {
            }
        }
    }

    public static String getProperty(String key){
        if(null == props) {
            loadProps();
        }
        return props.getProperty(key);
    }

    public static void main(String[] args) {
        String http = getProperty("path");
        System.out.println(http);
    }
    public static String getProperty(String key, String defaultValue) {
        if(null == props) {
            loadProps();
        }
        return props.getProperty(key, defaultValue);
    }
  /*  public static String getProperty(String key) {
        String value = "";

        Properties props = new Properties();

        try {
            InputStream input = PropUtil.class.getResourceAsStream("/config.properties");//web中可能用到的一些常见属性
            props.load(input);
//			Properties prop = PropertiesLoaderUtils.loadAllProperties("resources/jdbc.properties");
            value = props.getProperty(key);
        } catch (IOException e) {

            e.printStackTrace();
        }
        return value;
    }

    public static void setProperty(Map<String, String> data) {

        Properties props = new Properties();


        try {
            InputStream input = PropUtil.class.getResourceAsStream("/config.properties");

            props.load(input);

//			props.setProperty(key, value);
            if (data != null) {
                Iterator<Entry<String, String>> iter = data.entrySet().iterator();
                while (iter.hasNext()) {
                    Entry<String, String> entry = iter.next();
                    props.setProperty(entry.getKey().toString(), entry.getValue().toString());
                }
            }

            OutputStream out = new FileOutputStream("E:\\YoungJDK�汾����\\3\\Young\\src\\main\\resources\\config.properties");

            props.store(out, null);
            input.close();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }*/
}
